package com.lehuowujie.lehuomarket.ui;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.ashokvarma.bottomnavigation.BadgeItem;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.lehuowujie.lehuomarket.R;
import com.lehuowujie.lehuomarket.base.BaseActivity;
import com.lehuowujie.lehuomarket.fragment.CategoryFragment;
import com.lehuowujie.lehuomarket.fragment.HomeFragment;
import com.lehuowujie.lehuomarket.fragment.InformationFragment;
import com.lehuowujie.lehuomarket.fragment.PersonalFragment;
import com.lehuowujie.lehuomarket.fragment.ShopFragment;
import com.lehuowujie.lehuomarket.ui.login.LoginActivity;
import com.lehuowujie.lehuomarket.util.SharedPrefUtil;

public class MainActivity extends BaseActivity implements BottomNavigationBar.OnTabSelectedListener {

    private BadgeItem badgeItem;
    private BottomNavigationItem navigationItem;
    private BottomNavigationBar navigationBar;

    /**
     * 记录当前点击的是哪个菜单栏
     */
    private int mCurrentPosition = 0;

    @Override
    public int setContentViewRes() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        initBottomBar();
    }

    @Override
    public void initEvent() {

    }

    /**
     * 初始化底部菜单栏
     */
    private void initBottomBar() {
        //消息个数
        badgeItem = new BadgeItem()
                .setBorderWidth(4)
                .setBackgroundColor(Color.RED)
                .setText("0")
                .setHideOnSelect(true);
        navigationItem = new BottomNavigationItem(R.mipmap.xx_on, "消息").setBadgeItem(badgeItem);
        navigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
        navigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        navigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);
        navigationBar.setActiveColor("#FFFFFF").setInActiveColor("#ABABAB").setBarBackgroundColor("#E31939");
        navigationBar.addItem(new BottomNavigationItem(R.mipmap.nav_1sy_off, "首页"))
                .addItem(new BottomNavigationItem(R.mipmap.nav_2jp_off, "分类"))
                .addItem(new BottomNavigationItem(R.mipmap.nav_3fl_off, "购物车"))
                .addItem(navigationItem)
                .addItem(new BottomNavigationItem(R.mipmap.nav_5wd_off, "我的"))
                .setFirstSelectedPosition(0).initialise();
        navigationBar.setTabSelectedListener(this);
        //设置默认显示的fragment
        setDefaultFragment();
    }

    /**
     * 设置默认fragment
     */
    private void setDefaultFragment() {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.fragmentLayout, HomeFragment.getInstance()).commit();
    }

    /**
     * 点击底部菜单栏的时候显示或者是隐藏fragment
     *
     * @param position 从0开始计数
     */
    private void showOrHideFragment(int position) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        switch (position) {
            case 0:
                transaction.replace(R.id.fragmentLayout, HomeFragment.getInstance());
                break;
            case 1:
                transaction.replace(R.id.fragmentLayout, CategoryFragment.getInstance());
                break;
            case 2:
                transaction.replace(R.id.fragmentLayout, ShopFragment.getInstance());
                break;
            case 3:
                transaction.replace(R.id.fragmentLayout, InformationFragment.getInstance());
                break;
            case 4:
                transaction.replace(R.id.fragmentLayout, PersonalFragment.getInstance());
                break;
        }
        transaction.commit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (SharedPrefUtil.getInstance(this).getIsLogined()) {
            navigationBar.selectTab(mCurrentPosition);
            showOrHideFragment(mCurrentPosition);
        }
        //获取当前选中项的position
        mCurrentPosition = navigationBar.getCurrentSelectedPosition();
    }

    @Override
    public void onTabSelected(int position) {
        if (SharedPrefUtil.getInstance(this).getIsLogined()) {
            showOrHideFragment(position);
        } else {
            if (position == 0 || position == 1) {
                showOrHideFragment(position);
            } else {
                //没有登录，进入登录页面
                navigationBar.selectTab(mCurrentPosition);
                startActivity(new Intent(this, LoginActivity.class));
            }
        }
        mCurrentPosition = position;
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }
}
